package hessian01;

import com.caucho.hessian.io.Hessian2Input;
import com.caucho.hessian.io.Hessian2Output;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

/**
 * Created by yuanhailong on 2021/10/30.
 */
public class HessianUtils {

    public static void main(String[] args) {
    }


    public static byte[] serialize(Object o) throws Exception{
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        Hessian2Output hessian2Output=new Hessian2Output(baos);
        hessian2Output.writeObject(o);
        hessian2Output.flush();
        byte[] bytes = baos.toByteArray();
        baos.close();
        hessian2Output.close();
        return bytes;
    }


    public static Object deserialize(byte[] b) throws Exception{
        ByteArrayInputStream bais=new ByteArrayInputStream(b);
        Hessian2Input hessian2Output=new Hessian2Input(bais);
        Object o = hessian2Output.readObject();
        bais.close();
        hessian2Output.close();
        return o;
    }

}
